layui.use(['table','common','jquery','laytpl','layer','form'], function(){
    var table = layui.table,
        common=layui.common,
        $=layui.jquery,
        laytpl = layui.laytpl,
        form = layui.form,
        layer = layui.layer;
    table.render({
         elem: '#data'
        ,url:'/system/user/userList'
        ,cols: [[
            {type:'checkbox'},
            {field:'id', width:80, title: 'ID', sort: true}
            ,{field:'account', width:80, title: '账号'}
            ,{field:'name', width:80, title: '昵称', sort: true}
            ,{field:'sex', width:80, title: '性别',
                templet: function(d){
                    return d.sex=='1'?'男':'女';
                }}
            ,{field:'email', title: '邮箱', minWidth: 100}
            ,{field:'phone', title: '手机',  minWidth: 100}
            ,{field:'createTime', minWidth:80, title: '创建时间', sort: true,
                templet: function(d){
                   return common.dateFormat(new Date(d.createTime),'yyyy-MM-dd hh:mm:ss');
                }
             }
            ,{field:'status', width:80, title: '状态',
                templet: function(d){
                    var text;
                    if(d.status=='1'){
                        text="<i title='启用' class='layui-icon layui-icon-circle-dot green'></i>";
                    }else{
                        text="<i title='冻结' class='layui-icon layui-icon-circle-dot gray'></i>";
                    }
                    return text;
                }
            }
/*
            ,{fixed: 'right', title:'操作', width:150, align:'center', toolbar: '#barOperate'}
*/
        ]]
        ,page: true
    });

    //自定义验证规则
    form.verify({
        name: function(value){
            if(value.length < 2){
                return '姓名至少得2个字符啊';
            }
        }
        ,password: [/(.+){6,12}$/, '密码必须6到12位']
        ,repassword: function(value){
            if($('#password').val()!=$('#repassword').val()){
                return '两次密码不一致';
            }
        }
    });

    //监听提交
    form.on('submit(add)', function(data){
        var field=data.field;
        var ids=[];
        var params={};
        for(var obj in field){
            if(obj.indexOf("roleId")!=-1){
                ids.push(field[obj]);
            }else {
                params[obj]=field[obj];
            }
        }
        params.roleIds=ids.join(",");
        common.post("/system/user/saveUser",params,function (data) {
            if(data.code==0){
                layer.alert("增加成功", {icon: 6},function () {
                    // 获得frame索引
                    var index = parent.layer.getFrameIndex(window.name);
                    //关闭当前frame
                    parent.layer.close(index);
                });
            }
        });
        return false;
    });

    //监听表格复选框选择
    table.on('checkbox(user)', function(obj){
        console.log(obj)
    });
   /* table.on('tool(user)', function(obj){
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的DOM对象
        if(layEvent === 'detail'){ //查看
            //do somehing
        } else if(layEvent === 'del'){ //删除
            layer.confirm('真的删除行么', function(index){
                obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                layer.close(index);
                //向服务端发送删除指令
            });
        } else if(layEvent === 'edit'){ //编辑
            //do something

            //同步更新缓存对应的值
            obj.update({
                username: '123'
                ,title: 'xxx'
            });
        }
    });*/
    var $ = layui.$, active = {
        saveUser: function(){ //添加
            //查询角色列表
            common.post("/system/role/roleListAll",{},function (data) {
                if(data.code==0){
                    var data={
                        title:"角色",
                        list:data.data
                    };
                    var getTpl = roleTpl.innerHTML
                        ,view = document.getElementById('roleView');
                    laytpl(getTpl).render(data, function(html){
                        view.innerHTML = html;
                    });
                    layui.form.render('checkbox');
                    layer.open({
                        type: 1
                        ,title: '用户新增'
                        ,area: ['550px', '420px']
                        ,shade: [0.3, '#393D49']
                        ,content: $("#showForm")
                        ,zIndex: layer.zIndex
                        ,success: function(layero){
                            layer.setTop(layero);
                        }
                    });
                }
            });
        }
        ,getCheckLength: function(){ //获取选中数目
            var checkStatus = table.checkStatus('data')
                ,data = checkStatus.data;
            layer.msg('选中了：'+ data.length + ' 个');
        }
        ,isAll: function(){ //验证是否全选
            var checkStatus = table.checkStatus('data');
            layer.msg(checkStatus.isAll ? '全选': '未全选')
        }
    };

    $('.demoTable .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
});